package com.foreknow.elm.service.impl;

import com.foreknow.elm.dao.CartDao;
import com.foreknow.elm.dao.DeliveryAddressDao;
import com.foreknow.elm.dao.OrderDetailetDao;
import com.foreknow.elm.dao.OrdersDao;
import com.foreknow.elm.dao.impl.CartDaoImpl;
import com.foreknow.elm.dao.impl.DeliveryAddressDaoImpl;
import com.foreknow.elm.dao.impl.OrderDetailetDaoImpl;
import com.foreknow.elm.dao.impl.OrdersDaoImpl;
import com.foreknow.elm.po.Cart;
import com.foreknow.elm.po.DeliveryAddress;
import com.foreknow.elm.po.OrderDetailet;
import com.foreknow.elm.po.Orders;
import com.foreknow.elm.service.OrdersService;
import com.foreknow.elm.util.DBUtil;

import java.util.ArrayList;
import java.util.List;

import static com.foreknow.elm.util.CommonUtil.getCurrentDate;

public class OrdersServiceImpl implements OrdersService {
    private OrdersDao orderDao = new OrdersDaoImpl();
    private OrderDetailetDao detDao = new OrderDetailetDaoImpl();

    @Override
    public int createOrders(Orders orders) {
        CartDao cartDao = new CartDaoImpl();
        Cart cart = new Cart();
        int orderId = 0;
        try {
            List<Cart> list = cartDao.listCart(orders.getUserId(), orders.getBusinessId());
            Orders order = new Orders();
            order.setUserId(orders.getUserId());
            order.setBusinessId(orders.getBusinessId());
            order.setOrderDate(getCurrentDate());
            order.setOrderTotal(orders.getOrderTotal());
            order.setDaId(orders.getDaId());
            order.setOrderState(orders.getOrderState());
            orderId = orderDao.createOrders(order);

            for (Cart c : list) {
                detDao.saveOrderDetailet(orderId, c.getFoodId(), c.getQuantity());

                cart.setUserId(orders.getUserId());
                cart.setBusinessId(c.getBusinessId());
                cart.setFoodId(null);
                cartDao.removeCart(cart);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            DBUtil.close();
        }
        return orderId;
    }

    @Override
    public int updateOrders(Integer orderId, Integer orderState) {
        int row = 0;
        try {
            DBUtil.getConnection();
            row = orderDao.updateOrders(orderId, orderState);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            DBUtil.close();
        }
        return row;
    }

    @Override
    public Orders getOrdersById(Integer orderId) {
        Orders orders = new Orders();
        try {
            DBUtil.getConnection();
            orders = orderDao.getOrdersById(orderId);
            List<OrderDetailet> odList = detDao.listOrderDetailet(orders.getOrderId());
            orders.setList(odList);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            DBUtil.close();
        }
        return orders;
    }

    @Override
    public List<Orders> listOrdersByUserId(String userId) {
        List<Orders> list = new ArrayList<>();
        try {
            DBUtil.getConnection();
            list = orderDao.listOrdersByUserId(userId);
            for (Orders o : list) {
                List<OrderDetailet> odList = detDao.listOrderDetailet(o.getOrderId());
                o.setList(odList);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            DBUtil.close();
        }
        return list;
    }
}
